Custom parameters
Sometimes when creating custom commands you might want to use a custom parameter as well.
By default these are the parameters already supported:
- ParameterBool
- ParameterEnum
- ParameterFloat
- ParameterGameObject
- ParameterInt
- ParameterScene
- ParameterString
With these you can do pretty much everything, let's say we want to change the layer of a GameObject. We could easily just use the ParameterString however this would also allow for non-existing layers to be parsed.
So let's create our own ParameterLayer instead.
using ExtensionTools.Console.Commands.Parameters;
public class ParameterLayer : Parameter
{
public override bool TryParse(string parameter, out object parsedObject)
{
int Layer = LayerMask.NameToLayer(parameter.Replace("\"",""));
parsedObject = Layer;
if (Layer > -1) //Check if it exists, Parsing successful
{
return true;
}
return false;//Parsing not successful
}
}
Make sure you remove the quotes from strings which might have white space!
As you can see not much is needed to create a custom Parameter. However we can also add a ConsoleSuggestionWindow which popups and gives us a list of all the layers to choose from!
Custom ConsoleSuggestionWindow
Adding a SuggestionWindow for the Layers is simply done by creating a new class and inheriting from ConsoleSuggestionWindow.
using System.Collections.Generic;
using UnityEngine;
using ExtensionTools.Console.Suggestions;
public class LayerConsoleSuggestionWindow : ConsoleSuggestionWindow
{
List<string> m_Suggestions = new List<string>();
public override void InitializeWindow()
{
m_Suggestions.Clear();
//Add all layers
for (int i = 0; i <= 31; i++) //unity supports 31 layers
{
var layerName = LayerMask.LayerToName(i);
if (layerName.Length > 0) //Get length of the layername
m_Suggestions.Add("\""+layerName+"\"");
}
}
protected override List<string> GetSuggestions(string currentString)
{
return m_Suggestions;
}
}
Now we can go back and add this suggestion window to the constructor of our ParameterLayer.
public class ParameterLayer : Parameter
{
public ParameterLayer() : base(new LayerConsoleSuggestionWindow())
{
}
...
}